package mao.leetcode.dp.leetcode5;

public class leetcode5 {

    public String longestPalindrome(String s) {
        if (s.trim().equals("")) return s;
        int start=0,end=0;
        for (int i = 0; i < s.length(); i++) {
            int len1 = expendAroundCenter(s, i, i);
            int len2 = expendAroundCenter(s, i, i + 1);
            int maxLen = Math.max(len1, len2);
            if (maxLen > end-start){
                start=i-(maxLen-1)/2; //这里对奇数情况和偶数情况做统一处理。
                end=i+maxLen/2;
            }
        }
        return s.substring(start,end+1);
    }

    public int expendAroundCenter(String s, int start, int end){
        while (start>=0 &&end<s.length() && s.charAt(start) == s.charAt(end)){
            start--;
            end++;
        }
        return end-start-1;  //返回长度
    }
}
